package com.xbongbong.paas.service.workflow.setting.impl;

import com.alibaba.fastjson.JSON;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.config.enums.WorkflowTriggerMethodEnum;
import com.xbongbong.paas.pojo.workflow.NodeCoreConfigurationPojo;
import com.xbongbong.paas.pojo.workflow.WorkflowNodeTreePojo;
import com.xbongbong.paas.pojo.workflow.nodes.vo.DeleteRecordNodeVO;
import com.xbongbong.paas.pojo.workflow.nodes.vo.WorkflowNodeGetVO;
import com.xbongbong.paas.pojo.workflow.settings.WorkflowGetPojo;
import com.xbongbong.paas.pojo.workflow.settings.vo.WorkflowEnableVO;
import com.xbongbong.paas.service.workflow.setting.AbstractWorkflowNodeSettings;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.ProErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.workflow.domain.entity.WorkflowNodeEntity;
import com.xbongbong.workflow.domain.entity.ext.WorkflowNodeEntityExt;
import com.xbongbong.workflow.enums.WorkflowNodeTypeEnum;
import com.xbongbong.workflow.model.WorkflowNodeModel;
import com.xbongbong.workflow.pojo.nodes.DeleteRecordNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2021/05/21 14:35
 */
@Service("workflowNodeDeleteSettings")
public class WorkflowNodeDeleteSettings extends AbstractWorkflowNodeSettings {

    private static String CONSTANT = "constant.workflowNode.delete_data_node_content";

    private static final Logger LOGGER = LoggerFactory.getLogger(WorkflowNodeDeleteSettings.class);

    @Resource
    private WorkflowNodeModel workflowNodeModel;

    @Override
    public Integer type() {
        return WorkflowNodeTypeEnum.DELETE_NODE.getType();
    }

    @Override
    public WorkflowNodeTreePojo getContent(WorkflowNodeEntity workflowNodeEntity, WorkflowGetPojo workflowGetPojo) throws XbbException {
        WorkflowNodeTreePojo workflowNodeTreePojo = new WorkflowNodeTreePojo();
        try {
            List<String> list = getNodeContent(workflowGetPojo, workflowNodeEntity, CONSTANT);

            workflowNodeTreePojo.setContent(list);
        } catch (Exception e) {
            LOGGER.error("WorkflowNodeDeleteSettings.getContent error", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
        return workflowNodeTreePojo;
    }

    @Override
    public WorkflowNodeGetVO get(WorkflowNodeEntity workflowNodeEntity) throws XbbException {
        try {
            DeleteRecordNode deleteRecordNode = JSON.parseObject(workflowNodeEntity.getConfigData(), DeleteRecordNode.class);
            NodeCoreConfigurationPojo nodeCoreConfigurationPojo = new NodeCoreConfigurationPojo();
            DeleteRecordNodeVO deleteRecordNodeVO = getDeleteRecordNode(workflowNodeEntity, deleteRecordNode);
            nodeCoreConfigurationPojo.setDeleteRecordNode(deleteRecordNodeVO);

            String formName = getFormName(workflowNodeEntity, workflowNodeEntity.getCorpid());
            WorkflowNodeGetVO workflowNodeGetVO = new WorkflowNodeGetVO(formName, workflowNodeEntity.getAppId(), workflowNodeEntity.getFormId(), workflowNodeEntity.getMenuId(), workflowNodeEntity.getSaasMark(), workflowNodeEntity.getBusinessType(),
                    workflowNodeEntity.getUid(), workflowNodeEntity.getId(), workflowNodeEntity.getPrevId(), workflowNodeEntity.getWrapperId(), nodeCoreConfigurationPojo);
            List<Integer> nodeTypeList = Arrays.asList(WorkflowNodeTypeEnum.START_NODE.getType(), WorkflowNodeTypeEnum.CREATE_NODE.getType(),
                    WorkflowNodeTypeEnum.SINGLE_DATA_NODE.getType(), WorkflowNodeTypeEnum.MULTI_DATA_NODE.getType());
            workflowNodeGetVO.setNodeList(getBeforeNodeListByNodeType(workflowNodeEntity.getCorpid(), workflowNodeEntity.getWorkflowId(), workflowNodeEntity.getId(), nodeTypeList));
            return workflowNodeGetVO;
        } catch (Exception e) {
            LOGGER.error("WorkflowNodeDeleteSettings.get error", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
    }

    private DeleteRecordNodeVO getDeleteRecordNode(WorkflowNodeEntity workflowNodeEntity, DeleteRecordNode deleteRecordNode) {

        if (Objects.isNull(deleteRecordNode) || Objects.isNull(deleteRecordNode.getWorkflowNodeId())) {
            return null;
        }
        String nodeName = "";
        String corpid = workflowNodeEntity.getCorpid();
        Integer delete = 1;
        WorkflowNodeEntity workflowNodeEntity1 = workflowNodeModel.getByKey(deleteRecordNode.getWorkflowNodeId(), corpid);
        if (Objects.nonNull(workflowNodeEntity1)) {
            nodeName = workflowNodeEntity1.getName();
            delete = 0;
        }
        return new DeleteRecordNodeVO(deleteRecordNode.getWorkflowNodeId(), deleteRecordNode.getTriggerNodeFlag(), nodeName, delete);
    }

    @Override
    public WorkflowEnableVO check(WorkflowNodeEntity workflowNodeEntity, Map<Long, WorkflowNodeEntity> nodeMap, WorkflowGetPojo workflowGetPojo) throws XbbException {
        WorkflowEnableVO workflowEnableVO = new WorkflowEnableVO();
        DeleteRecordNode deleteRecordNode = JSON.parseObject(workflowNodeEntity.getConfigData(), DeleteRecordNode.class);
        if (Objects.equals(workflowNodeEntity.getTriggerType(), WorkflowTriggerMethodEnum.FORM.getType())) {
            if (Objects.isNull(deleteRecordNode.getWorkflowNodeId()) || Objects.equals(deleteRecordNode.getWorkflowNodeId(), 0L)) {
                workflowEnableVO.setSuccess(0);
                workflowEnableVO.setWorkflowNodeId(workflowNodeEntity.getId());
                workflowEnableVO.setMessage(I18nMessageUtil.getMessage(ProErrorCodeEnum.API_ERROR_260042.getMsg()));
                return workflowEnableVO;
            }
            WorkflowNodeEntity node = nodeMap.get(deleteRecordNode.getWorkflowNodeId());
            if (!equals(workflowNodeEntity.getFormId(), workflowNodeEntity.getBusinessType(), node.getFormId(), node.getBusinessType())) {
                workflowEnableVO.setSuccess(0);
                workflowEnableVO.setWorkflowNodeId(workflowNodeEntity.getId());
                workflowEnableVO.setMessage(I18nMessageUtil.getMessage(ProErrorCodeEnum.API_ERROR_260042.getMsg()));
                return workflowEnableVO;
            }
        }
        return workflowEnableVO;
    }

    @Override
    public void replaceWorkflowNodeId(WorkflowNodeEntityExt workflowNodeEntityExt, Map<Long, Long> workflowNodeMap) throws XbbException {
        DeleteRecordNode deleteRecordNode = JSON.parseObject(workflowNodeEntityExt.getConfigData(), DeleteRecordNode.class);
        if (Objects.nonNull(deleteRecordNode)) {
            deleteRecordNode.setWorkflowNodeId(workflowNodeMap.get(deleteRecordNode.getWorkflowNodeId()));
            workflowNodeEntityExt.setConfigData(JSON.toJSONString(deleteRecordNode));
        }
    }
}

